<?php
namespace Application\pform\a_wan;

use Application\model\BaseLoginModel;
use Cola\Cola;

/**
 * 登录模型
 *
 * Login下有如下参数
 * @property string $channel 玩心渠道子包名
 * @property string $xx_game_id 渠道子包名
 * @property string $version 版本号
 * @property string $deviceCode 设备码
 * @property string $verify 临时通行码
 * @property string $ext_info 透传字段
 * @property $string $device_info
 */
class LoginModel extends BaseLoginModel
{

    protected $v = 1;
    /**
     * 初始化登录参数
     * @return void
     */
    protected function initLoginParams()
    {
        // 跟平台验证玩家数据
        $res = $this->oauth();
        if (!$res) {
            error('oauth fail');
            return false;
        }
        // 账号
        if($this->v==2){
            $this->account = $res['fx_user_id'];
        }else{
            $this->account = $res['user_id'];
        }

        $this->account_name = $res['user_name'];
        $this->cpid = $this->xx_game_id;
        $this->ccid = $this->channel;
        $this->device_info = $this->device_info;
    }

    private function oauth()
    {
        $res = $this->DEVLoginInfo();
        if($res) {
            $this->content = $res;
            return $res;
        };
        $params = [];

        $params['xx_game_id'] = $this->xx_game_id;
        if($this->v==2){
            $params['timestamp'] = time();
            $params['device_info'] = $this->device_info;
            $params['channel_id'] = $this->channel;
        }else{
            $params['channel'] = $this->channel;
            $params['time'] = time();
        }

        $params['ip'] = ip();
        $params['device_id'] = $this->deviceCode;
        $params['ext_info'] = $this->ext_info;
        $params['authorize_code'] = $this->verify;
        if($this->v==2){
            list($before,$params['sign']) = PublicModel::signV21($params,SERVER_KEY,['ext_info','device_info']);
        }else{
            $params['sign'] = $this->sign($params);
        }
        $this->logfile("v===".$this->v);
        $this->logfile("before==".($before ?? ''));
        $this->logfile("url==".OAUTH_TOKEN_URL);
        $res = curl(OAUTH_TOKEN_URL, $params);
        $res = json_decode($res, true);
        $this->logfile(json_encode($params).' res: '.json_encode($res));
        // 成功
        if (isset($res['ret']) && $res['ret'] == 1) {
            $content = $res['content'];
            $this->content = $res['content'];
            $this->logfile("account:{$content['user_id']} check channel sign success");
            return $content;
        }else {
            $this->logfile('check channel sign error. params'.json_encode($params).' res: '.json_encode($res));
            $this->msg = $res['msg'];
            return false;
        }
    }



    private function sign($params)
    {
        // 加入xx_game_secret
        $params['xx_game_secret'] = GAME_SECRET;
        // 排序
        ksort($params);
        // 生成字符串
        $signStr = "";
        foreach ($params as $key=>$value) {
            // 值为空的不参与签名
            if (empty($value)) {
                continue;
            }
            $signStr .= "&{$key}={$value}";
        }
        $signStr = substr($signStr, 1);
        // 请求中不要加入xx_game_secret
        unset($params['xx_game_secret']);
        // md5获得签名
        return md5($signStr);
    }

    public function channelInfo()
    {
        if($this->v==2){
            return $this->content;
        }
        return [
            'userId' => $this->content['user_id'],
            'userName' => $this->content['user_name'],
            'accessToken' => $this->content['access_token'],
            'isFatigue' => $this->content['fatigue_state'],
            'isAdult' => $this->content['is_adult'],
            'isHiddenPay' => $this->content['pay_class'],
        ];
    }
    
    /**
     * 服务器列表数据
     * @return void
     */
    protected function initServerListParams()
    {

    }

}
